#include<bits/stdc++.h>
#define ll long long
template <typename T> inline void read(T &x) {
    x = 0; char ch = getchar(); int f = 1;
    while (!isdigit(ch) && ch ^ '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f;
}
using namespace std;
const int N=1505;
int n,m,v[N],len[N];
char S[N],s[N][N];
double ans;
string anss;
void dfs(int k,string st)
{
	if(k>n)
	{
		double tot=0;
		int cnt=0;
		for(int a=1;a<=m;a++)
		{
			for(int b=1;b+len[a]-1<=n;b++)
			{
				bool ck=1;
				for(int c=0;c<len[a];c++)
				{
					if(st[b+c]!=s[a][c+1])
					{
						ck=0;
						break;
					}
				}
				if(ck==1)
				{
					cnt++;
					tot+=log2(v[a]);
				}
			}
		}
		if(cnt==0)
		{
			if(ans<1) ans=1,anss=st;
			return;
		}
		if(ans<tot/cnt) ans=tot/cnt,anss=st;
		return;
	}
	if(S[k]=='.')
	{
		for(int a=0;a<=9;a++) dfs(k+1,st+(char)('0'+a));
	}
	else dfs(k+1,st+S[k]);
}
int main()
{
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
    scanf("%d%d%s",&n,&m,S+1);
    for(int a=1;a<=m;a++)
    {
    	scanf("%s%d",s[a]+1,&v[a]);
    	len[a]=strlen(s[a]+1);
	}
	dfs(1,"#");
	for(int a=1;a<=n;a++) cout<<anss[a];
}
